<?php

namespace App\Http\Controllers\Common;

use App\Http\Classes\Book;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class BookController extends Controller
{
    //
    public function getBooksByMaterial(Request $request)
    {
        try
        {
            $material = $request->has('material') ? $request->get('material') : 1;
            $material = is_int($material) ? $material : (int)$material;
            $book = new Book();
            $books = $book->getBooksByMaterialId($material);
            if ( $books )
            {
                return $this->response(1, '获取成功', $books);
            }
            else
            {
                return $this->response(0, '无匹配数据', [], $book->getMessage());
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    public function getAllBooks()
    {
        try
        {
            $book = new Book();
            $books = $book->getAllBooks();

            return $this->response(1, '获取成功', $books);
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }
}
